//
//  MonthController.m
//  1100163班第六组随手记
//
//  Created by 邵瑞 on 13-6-18.
//
//

#import "MonthController.h"
#import "BIDClockVC.h"
#import "BIDTypesManager.h"
#import "BIDDateView.h"
#import "BIDBillsLookoverManager.h"
@interface MonthController ()
{
       NSMutableArray * monthArray;
       NSInteger monthInteger;
}
@end

@implementation MonthController
@synthesize year;
- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}
-(void)viewWillAppear:(BOOL)animated
{
        self.navigationController.navigationBarHidden= NO;
//       [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"open.png"] forBarMetrics:UIBarMetricsDefaultsav];
//       self.navigationController.navigationBar.shadowImage = [UIImage imageNamed:@"open.png"];

       UIBarButtonItem *itern=  [[UIBarButtonItem alloc] initWithTitle:@"删除" style:0 target:self action:@selector(tableViewEdit:)];
       self.navigationItem.rightBarButtonItem=itern;

}

- (void)viewDidLoad
{

              
       monthArray =[[NSMutableArray alloc] init];
       monthArray = [[BIDTypesManager shareTypesManager] aYearOfMonthTotalBills:year];
 
       self.title = [NSString stringWithFormat:@"%d",year];
//self.tableView.backgroundColor = [UIColor grayColor];
       [self.tableView reloadData];
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
 
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [monthArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
              UIButton * lable = [[UIButton alloc] init];
              UILabel * monthLable = [[UILabel alloc] init];
                     if (cell == nil)
                     {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
              lable.frame = CGRectMake(160, 5, 160, 35);

               lable = [[UIButton alloc] initWithFrame:CGRectMake(110, 5, 150, 35)];
              lable.tag = 10;
              lable.titleLabel.textAlignment = NSTextAlignmentCenter;
              lable.titleLabel.textColor = [UIColor redColor];
              lable.backgroundColor = [UIColor clearColor];
              [cell addSubview:lable];
              lable = (UIButton *)[cell viewWithTag:10];
             monthLable.frame = CGRectMake(10, 5, 90, 35);
              monthLable.tag = 11;
              monthLable.font  =[UIFont fontWithName:@"Arial" size:30];
              monthLable.textAlignment = NSTextAlignmentCenter;
              monthLable.textColor = [UIColor brownColor];
              //        lable.tintColor = [UIColor  whiteColor];
              monthLable.backgroundColor = [UIColor whiteColor];
              monthLable.userInteractionEnabled = YES;

              [cell addSubview:monthLable];
              
              monthLable = (UILabel *)[cell viewWithTag:11];

    }

      
   NSInteger month =  [[monthArray objectAtIndex:indexPath.row]integerValue];
   
   NSString * monthstr = [NSString stringWithFormat:@"%d月",month];
    NSInteger amount =[[BIDBillsLookoverManager shareBillsLookoverManager] totalAmountCountInMonth:month andYear:year];
       NSString * aTotalMonthamount = [NSString stringWithFormat:@"总%d金额元",amount ];
       [lable setTitleColor:[UIColor brownColor] forState:UIControlStateNormal];
    [lable setTitle:aTotalMonthamount forState:UIControlStateNormal];
//    [lable addTarget:self action:@selector(nextDeatil) forControlEvents:UIControlEventTouchUpInside];
    monthLable.text  = monthstr;

    // Configure the cell...
    return cell;
    [lable release];
    [monthLable release];
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
               monthInteger = [[monthArray objectAtIndex:indexPath.row] integerValue];
              [self uialetrview];


                     
}
-(void)uialetrview
{
       NSString * deleteMonthBills = [NSString stringWithFormat:@"是否删除%d月的帐单",monthInteger];
              
       UIAlertView * alter = [[UIAlertView alloc] initWithTitle:@"提醒" message:deleteMonthBills delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
       [alter show];
       [alter release];
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
        switch (buttonIndex)
  {
        case 0:
              [self deleteYearOrMonth];
              NSLog(@"Cancel Button Pressed");
            break;
        case 1:
            NSLog(@"Button 1 Pressed");
            break;
                default:
            break;
            }
      
}
-(void)deleteYearOrMonth
{
                     
       [[BIDTypesManager shareTypesManager] deleteAMonthBill:monthInteger inYear:year];
              monthArray = [[BIDTypesManager shareTypesManager] aYearOfMonthTotalBills:year];

       [self.tableView reloadData];


}


//删除按钮
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
       return @"删除";
}
- (void)tableViewEdit:(id)sender
{
//   [self.tableviews setEditing:!self.tableviews.editing animated:YES];
    if (self.tableView.editing)
      {
  self.tableView.editing=NO;
      }
     else
     {self.tableView.editing=YES;
     };
}
-(void)nextDeatil
{
       BIDDateView* nextpage = [[BIDDateView alloc] init];
              nextpage.detailYear = year;
       [self.navigationController pushViewController:nextpage animated:YES];
       
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    /*
     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];
     [detailViewController release];
     */
}

@end
